//
//  TimeUtils.m
//  HomeImprovement
//
//  Created by C C on 14-9-13.
//  Copyright (c) 2014年 IWork. All rights reserved.
//

#import "TimeUtils.h"

@implementation TimeUtils

+(NSString *)GetDescriptionTime:(NSDate *)timestamp ISType:(BOOL)Istype{
//    NSArray *weekArray = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDate *nowDate = [NSDate date];
    NSDateComponents *comps = [calendar components:unitFlag fromDate:timestamp];
    NSDateComponents *nowComps = [calendar components:unitFlag fromDate:nowDate];
    
    NSString *TimeStr;
    if (Istype) {
        if ([comps year] < [nowComps year]) {
            TimeStr = [NSString stringWithFormat:@"%d年前",(int)([nowComps year] - [comps year])];
        }else if ([comps year] == [nowComps year] && [comps month] < [nowComps month]){
            TimeStr = [NSString stringWithFormat:@"%d个月前",(int)([nowComps month] - [comps month])];
        }else if ([comps year] == [nowComps year] && [comps month] == [nowComps month] && [comps day] < [nowComps day]){
            TimeStr = [NSString stringWithFormat:@"%d天前",(int)([nowComps day] - [comps day])];
        }else if ([comps year] == [nowComps year] && [comps month] == [nowComps month] && [comps day] == [nowComps day] && [comps hour] < [nowComps hour]){
            TimeStr = [NSString stringWithFormat:@"%d小时前",(int)([nowComps hour] - [comps hour])];
        }else if ([comps year] == [nowComps year] && [comps month] == [nowComps month] && [comps day] == [nowComps day] && [comps hour] == [nowComps hour] && [comps minute] < [nowComps minute]){
            long hour = [nowComps minute] - [comps minute];
            if (hour > 3) {
                TimeStr = [NSString stringWithFormat:@"%ld分钟前",hour];
            }else{
                TimeStr = @"刚刚";
            }
        }
    }else{
        if (([comps year] == [nowComps year] && [comps month] == [nowComps month] && [comps day] < [nowComps day]) || [comps day] == [nowComps day]) {
            long day = [nowComps day] - [comps day];
            switch (day) {
                case 0:
                    TimeStr = [NSString stringWithFormat:@"今天 %@", [self ToTimeFormat:timestamp TimeFormat:@"hh:mm"]];
                    break;
                case 1:
                    TimeStr = [NSString stringWithFormat:@"昨天 %@", [self ToTimeFormat:timestamp TimeFormat:@"hh:mm"]];
                    break;
                case 2:
                    TimeStr = [NSString stringWithFormat:@"前天 %@", [self ToTimeFormat:timestamp TimeFormat:@"hh:mm"]];
                    break;
                case 3:
                    TimeStr = [NSString stringWithFormat:@"%@",[self ToTimeFormat:timestamp TimeFormat:@"MM-dd hh:mm"]];
                    break;
                default:
                    break;
            }
        }
    }
    
    return TimeStr;
}

/*
 yyyy-MM-dd
 hh:mm
 yyyy-MM-dd hh:mm
 MM月dd日
 MM月dd日 hh:mm
 yyyy年MM月dd日 hh:mm
*/

+(NSString *)ToTimeFormat:(NSDate *)time TimeFormat:(NSString *)StrFormat{
    NSDateFormatter *mDateFormatter = [[NSDateFormatter alloc]init];
    [mDateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [mDateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [mDateFormatter setDateFormat:StrFormat];
     return [mDateFormatter stringFromDate:time];
}

#pragma mark --- 计算是星期几
+ (NSString *)getDay :(NSString *)day month:(NSString *)month year:(NSString *)year{
    NSArray *weekArray = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:[day intValue]];
    [comps setMonth:[month intValue]];
    [comps setYear:[year intValue]];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *date = [gregorian dateFromComponents:comps];
    NSDateComponents *weekdayComponents =
    [gregorian components:NSWeekdayCalendarUnit fromDate:date];
    NSInteger weekday = [weekdayComponents weekday];
    return weekArray[weekday-1];
}
+ (NSDate *)returnWeekFirstDay{
        NSDate *today = [NSDate date];
//    NSDate *today = [NSDate dateWithTimeIntervalSinceNow:-24*60*60*20];
    NSLog(@"Today date is %@",today);
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];// you can use your format.
    //Week Start Date
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:today];
    
    
    
    NSInteger dayofweek = [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:today] weekday];// this will give you current day of week
    
    [components setDay:([components day] - ((dayofweek) - 2))];// for beginning of the week.
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
    [dateFormat_first setDateFormat:@"yyyy-MM-dd"];
    NSString* dateString2Prev = [dateFormat stringFromDate:beginningOfWeek];
    
    NSDate* weekstartPrev = [dateFormat_first dateFromString:dateString2Prev];
    
    NSLog(@"StartDate:%@",weekstartPrev);
    
    return weekstartPrev;
    
}
+ (NSDate *)returnWeekLastDay{
    NSDate *today = [NSDate date];
//    NSDate *today = [NSDate dateWithTimeIntervalSinceNow:-24*60*60*20];
    NSLog(@"Today date is %@",today);
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];// you can use your format.
    
    //Week End Date
    
    NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *componentsEnd = [gregorianEnd components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:today];
    
    NSInteger Enddayofweek = [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:today] weekday];// this will give you current day of week
    
    [componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+1)];// for end day of the week
    
    NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd];
    NSDateFormatter *dateFormat_End = [[NSDateFormatter alloc] init];
    [dateFormat_End setDateFormat:@"yyyy-MM-dd"];
    NSString* dateEndPrev = [dateFormat stringFromDate:EndOfWeek];
    
    NSDate* weekEndPrev = [dateFormat_End dateFromString:dateEndPrev];
    NSLog(@"EndDate:%@",weekEndPrev);
    
    return weekEndPrev;
}


+ (NSString *)gobackTime:(NSString *)dataTime{
    
    NSString *year = [dataTime substringWithRange:NSMakeRange(0, 4)];
    NSString *month = [dataTime substringWithRange:NSMakeRange(5, 2)];
    NSString *day = [dataTime substringWithRange:NSMakeRange(8, 2)];
    NSString *week = [TimeUtils getDay:day month:month year:year]; // 周几
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *newsDateFormatted = [dateFormatter dateFromString:dataTime];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    NSDate* current_date = [[NSDate alloc] init];
    
    NSTimeInterval time=[current_date timeIntervalSinceDate:newsDateFormatted];//间隔的秒数
    int days=((int)time)/(3600*24);
    NSString *newTime = [NSString stringWithFormat:@"%@-%@ (%d天前 %@)",month,day,days,week];
    if (days < 0) {
        NSString *futureDay = [NSString stringWithFormat:@"%d",days];
        newTime = [NSString stringWithFormat:@"%@-%@ (%@天后 %@)",month,day,[futureDay substringFromIndex:1],week];
    }
    if (days == 0) {
        newTime = [NSString stringWithFormat:@"%@-%@ (今天 %@)",month,day,week];
    }
    return newTime;
}
@end
